home *** CD-ROM | disk | FTP | other *** search
- Path: locutus.rchland.ibm.com!usenet
- From: Philip Staite <pstaite@vnet.ibm.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Pointers HELP!!!!
- Date: Fri, 26 Jan 1996 08:36:37 -0600
- Organization: IBM Rochester, OS/2 Device Driver Team
- Message-ID: <3108E6F5.167E@vnet.ibm.com>
- References: <31055e1d.1020193@news.pi.se> <DLpK20.6Co@prl.research.philips.com>
- NNTP-Posting-Host: powertool.rchland.ibm.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (X11; I; AIX 1)
-
- Richard B Sagar wrote:
- >
- > You can only pass a pointer to a function if the function is a static
- > member function ... @.@ ... or is it a member function of a static class.
-
- Maybe in the case he/she was using... But in general, you can pass a
- pointer to any member. Ptrs to non static member functions just aren't
- very useful unless you have an instance to bind them to. But, if you've
- got an instance and a ptr to a mfn... Try the following example:
-
- #include<iostream.h>
- #include<stdlib.h>
- #include<time.h>
-
- class foo {
- public:
- foo() : fp( foo::bar ) {}
- void next() { fp = ( rand() & 4 ) ? foo::bar : foo::baz; }
- void run() { (this->*fp)(); }
- void bar() { cout << "foo::bar()" << endl; }
- void baz() { cout << "foo:baz()" << endl; }
- private:
- void (foo::*fp)();
- };
-
-
- void finish( foo* f, void (foo::*fp)() ) {
- cout << "Finished, calling last function..." << endl;
- (f->*fp)(); }
-
-
- int main() {
- srand( time( 0 ) );
- foo f;
- for( int i = 0 ; i++ < 10 ; f.next() )
- f.run();
- finish( &f, foo::bar );
- return 0; }
-
-
- --
-
- Phil Staite, (507) 253-2529, team OS/2
- internet: pstaite@vnet.ibm.com internal: pstaite@rchland
-